package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author: Joey
 * @Description: 菜品
 * @date:2024/4/15 13:36
 */
@RestController("DishController")
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    private void clearCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping()
    @ApiOperation("新增菜品")
    public Result<String> addNewDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品,{},{}",dishDTO.getName(),dishDTO.getDescription());
        dishService.insertDish(dishDTO);
        clearCache("dish:"+dishDTO.getCategoryId());
        return Result.success("成功添加");
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     * @return
     */
    @PutMapping()
    @ApiOperation("修改菜品信息")
    public Result<String> modifyDishInfo(@RequestBody DishDTO dishDTO){
        dishService.modifyDishInfo(dishDTO);
        clearCache("dish:*");
        return Result.success("修改成功");
    }


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> DishListPage(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.dishListPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping()
    @ApiOperation("删除菜品")
    public Result<String> DeleteDishBatch(@RequestParam List<Long> ids){
        dishService.deleteBatch(ids);
        clearCache("dish:*");
        return Result.success();
    }

    /**
     * 修改菜品售卖状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品售卖状态")
    public Result<String> modifyStatus(@PathVariable Integer status ,long id){
        log.info("禁用、启用 -> 菜品,{},{}",status,id);
        dishService.modifyStatus(status,id);
        clearCache("dish:*");
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> selectDishById(@PathVariable Integer id){
        log.info("菜品信息,{}",id);
        DishVO dish = dishService.selectDishById(id);
        return Result.success(dish);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List> selectDishByCateId(Integer categoryId){
        log.info("查询菜品信息-分类id,{}",categoryId);
        List<Dish> dish = dishService.selectDishByCateGoryId(categoryId);
        return Result.success(dish);
    }


}
